package viz;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;

import util.io.FileUtil;
import def.Argument;
import def.Event;

public class TriggerWord {
	public static void sameTSpanForDifferentEventTypes(String dataPath){
		String[] files = new File(dataPath).list();
		for (String file: files){
			if (!file.endsWith(".a2"))
				continue;
			Argument.clear();
			String[] lines = FileUtil.getTextFromFile(dataPath+file).split("\n");
			ArrayList<Argument> args = new ArrayList<Argument>();
			//======refactor the following to a separate method======
			for (String line: lines){
				Argument arg = Argument.parse(line);
				args.add(arg);
			}
			for (Argument arg: args){
				if(arg instanceof Event){
					Event e = (Event)arg;
					if (e.getTspan() == null){
						
						Event t = (Event)Argument.find(e.tid);
						if (t!=null){
							e.merge(t);
//							System.err.println("filling event with "+e.tid +" to " + t.tspan) ;
						}
						else
							System.err.println("sameTSpanForDifferentEventTypes  : Can't complete an event!");
					}
				}
			}
			//TODO remove tspan-only events
			for (Argument arg: args){
				if(arg instanceof Event){
					Event e = (Event)arg;
					if (e.eid != null){
						if (e.getTspan() == null)
							System.out.println("missing tspan in event "+e.tid);
					}
				}
			}
			//=========================
			
			// count same tspan appearing in two events
			Hashtable<String, String> tid2eventType  = new Hashtable<String, String>();
			HashSet<String> tidHasDiffTypes = new HashSet<String>();
			for (Argument arg: args){
				if(arg instanceof Event){
					Event e = (Event)arg;
					// TODO
					if (e.eid == null)
						continue;
					System.err.println(" EVENT:  "+e);
					if (tid2eventType.containsKey(e.tid)){
						if (!tid2eventType.get(e.tid).contains(e.eventType.name())){
							tid2eventType.put(e.tid, tid2eventType.get(e.tid)+","+e.eventType);
							tidHasDiffTypes.add(e.tid);
						}
					}
					else{
						tid2eventType.put(e.tid, e.eventType.name());
					}
				}
			}
			System.out.println(file);
			for (String tid: tidHasDiffTypes){
				System.out.println(tid+":"+tid2eventType.get(tid));
			}
			
		}
	}
	
	/**
	 * 
	 * @param path1: find .a2.t1
	 * @param path2: find .a2.t1
	 */
	public static void compareTriggerWords(String path1, String path2){
		int tp = 0,  fp = 0, fn = 0;
		for (String file : new File(path1).list()){
			if (file.endsWith("a2.t1")){
				
				ArrayList<Argument> args1 = getArgs(path1+file),
				args2 = getArgs(path2+file);
//				HashSet<String> words1 = getArgs(path1+file),
//				words2 = getArgs(path2+file);
				//TODO remove tspan-only events
				HashSet<String> words1 = new HashSet<String>(), words2 = new HashSet<String>();
				for (Argument arg: args1){
					if(arg instanceof Event){
						Event e = (Event)arg;
						if (e.eid != null){
							if (e.getTspan() == null)
								System.out.println("missing tspan in event "+e.tid);
							else
								words1.add(e.getTspan().text);
						}
					}
				}
				for (Argument arg: args2){
					if(arg instanceof Event){
						Event e = (Event)arg;
						if (e.eid != null){
							if (e.getTspan() == null)
								System.out.println("missing tspan in event "+e.tid);
							else
								words2.add(e.getTspan().text);
						}
					}
				}
				for (String word: words1){
					if (words2.contains(word)){
						tp++;
						words2.remove(word);
					}
					else{
						fn++;
					}
				}
				fp += words2.size();
			}
		}
		System.err.println((double)tp/(tp+fp)+" p");
		System.err.println((double)tp/(tp+fn) + " r");
		
	}
	
	public static void nestedEvent(String path1, String path2){
		int childCorrectAll = 0, childCorrectCorrect =0, childWrong = 0;
		for (String file : new File(path1).list()){
			if (file.endsWith("a2.t1")){
				
				ArrayList<Argument> args1 = getArgs(path1+file),
				args2 = getArgs(path2+file);
			}
		}
	}
	
	public static ArrayList<Argument> getArgs(String filename){
		Argument.clear();
		String[] lines = FileUtil.getTextFromFile(filename).split("\n");
		ArrayList<Argument> args = new ArrayList<Argument>();
		//======refactor the following to a separate method======
		for (String line: lines){
			Argument arg = Argument.parse(line);
			args.add(arg);
		}
		for (Argument arg: args){
			if(arg instanceof Event){
				Event e = (Event)arg;
				if (e.getTspan() == null){
					
					Event t = (Event)Argument.find(e.tid);
					if (t!=null){
						e.merge(t);
//						System.err.println("filling event with "+e.tid +" to " + t.tspan) ;
					}
					else
						System.err.println("sameTSpanForDifferentEventTypes  : Can't complete an event!");
				}
			}
		}
		
		return args;
	}
	
	
	public static void main(String[] args){
//		sameTSpanForDifferentEventTypes("data/BioNLP09/dev/");
//		compareTriggerWords("data/BioNLP09/dev/", "uturku/TurkuEventExtractionSystem-1.0/output2/geniaformat/");
	}
}
